package com.iknowone.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.iknowone.entity.Category;
import com.iknowone.entity.Contact;
import com.iknowone.entity.Coordinate;
import com.iknowone.entity.DbFieldDef;
import com.iknowone.entity.Poi;
import com.iknowone.entity.PoiInfo;
import com.iknowone.entity.Rating;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;

public class DbManager {

	private MongoConnector connector = MongoConnector.getInstance();
	private static Dao dao;

	static {
		dao = new Dao();
	}

	public Poi poiDetail(String poiId) {
		return tmpCreatePoi();
	}

	public List<Poi> search(String text) {
		DB db = connector.getDatabase();
		return dao.search(db, text);
	}

	public void save(Rating rating) {
		DBObject toSave = new BasicDBObject();
		toSave.put(DbFieldDef.USER_ID.getValue(), rating.getUserId());
		toSave.put(DbFieldDef.POI_ID.getValue(), rating.getPoiId());
		toSave.put(DbFieldDef.STARS.getValue(), rating.getStars());
		toSave.put(DbFieldDef.COMMENT.getValue(), rating.getComment());
		toSave.put(DbFieldDef.DATE.getValue(), rating.getDate());
		DB db = connector.getDatabase();
		dao.addRating(db, toSave);
	}

	public List<Poi> lastRecomandations(Boolean byFriends, Long limit) {
		return tmpCreatePoiList();
	}

	public List<Category> catList() {
		return tempcatList();
	}

	public List<Category> tempcatList() {
		List<Category> catList = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			if (i % 2 == 0) {
				Category category = new Category(i, "name " + i, "parent " + i);
				catList.add(category);
			} else {
				catList.add(new Category(i, "name " + i, null));
			}
		}
		return catList;
	}

	private Poi tmpCreatePoi() {
		Contact contact = new Contact("0740111111", "some adress in here",
				"your@email.com");
		Coordinate coord = new Coordinate(40, 23.4);
		PoiInfo info = new PoiInfo("schedule here");
		Category category = new Category(1, "category 1", null);
		Category category2 = new Category(2, "category 2", null);
		List<Category> categories = new ArrayList<Category>();
		categories.add(category);
		categories.add(category2);
		String comment = "aaaaaaaaaaaaa";
		Rating rating = new Rating("user id", "user Id", 1, comment, new Date());
		Rating rating2 = new Rating(" user id 2", "user id", 5,
				"BBBBBBBBBBBBBBBB", new Date());
		List<Rating> ratings = new ArrayList<>();
		ratings.add(rating);
		ratings.add(rating2);
		Map<Integer, Integer> ratingStats = new HashMap<>();
		ratingStats.put(1, 10);
		ratingStats.put(2, 30);
		ratingStats.put(3, 30);
		ratingStats.put(5, 20);
		Poi poi = new Poi("1", "name1", contact, categories, info, coord,
				ratings, 3.6, ratingStats);
		String pictureUrl = "pics/a.png";
		List<String> pics = new ArrayList<>();
		pics.add(pictureUrl);
		poi.setPictureUrl(pictureUrl);
		poi.setPictures(pics);
		return poi;
	}

	private List<Poi> tmpCreatePoiList() {

		List<Poi> pois = new ArrayList<>();
		Poi poi = tmpCreatePoi();
		pois.add(poi);
		return pois;
	}

}